package com.qo.qsdk.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.qo.qsdk.QSDK;
import com.qo.qsdk.base.IPay;
import com.qo.qsdk.base.IPayConsumeListener;
import com.qo.qsdk.base.IPayExecResultListener;
import com.qo.qsdk.base.IPayListener;
import com.qo.qsdk.base.PayError;
import com.qo.qsdk.base.PayType;
import com.qo.qsdk.http.QCommonResultBean;
import com.qo.qsdk.http.QResultCallback;
import com.qo.qsdk.http.QSDKHttp;
import com.qo.qsdk.model.VerifyResult;
import com.qo.qsdk.param.PayParams;
import com.qo.qsdk.ui.base.TipView;
import com.qo.qsdk.utils.LogManager;
import com.qo.qsdk.utils.QSDKHelper;

public class PayActivity extends BaseActivity {

    public static PayParams mPayParams;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoading();
        QSDKHttp.getInstance().paymentGenOrder(
                this,
                QSDKHelper.getInstance().getUserInfoModel(this).getToken(),
                mPayParams.getProductId(),
                mPayParams.getOrderNo(),
                mPayParams.getExtend(),
                mPayParams.getServerId(),
                mPayParams.getRoleId(),
                mPayParams.getRoleName(),
                new QResultCallback<QCommonResultBean<String>>() {
                    @Override
                    public void onSuccess(QCommonResultBean<String> response) {
                        IPay iPay = QSDK.getInstance().getPay(PayActivity.this);
                        if (iPay != null) {
                            iPay.pay(PayActivity.this, mPayParams.getProductId(), response.getBody(), new IPayListener() {
                                @Override
                                public void onCancel() {
                                    payCancel();
                                }

                                @Override
                                public void onSuccess(IPayExecResultListener iPayExecResultListener, PayType type, String orderNo, String purchaseToken) {
                                    reqVerifyPay(iPayExecResultListener, type, orderNo, purchaseToken);
                                }

                                @Override
                                public void onFail(PayError code, String msg) {
                                    payFail(code.ordinal(), msg);
                                }
                            });
                        }else{
                            payFail(1002, "pay error");
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        payFail(code, msg);
                    }
                }
        );
    }

    private void reqVerifyPay(IPayExecResultListener iPayExecResultListener, PayType type, String orderNo, String purchaseToken) {
        QSDKHttp.getInstance().verifyPay(this, purchaseToken, orderNo, new QResultCallback<QCommonResultBean<VerifyResult>>() {
            @Override
            public void onSuccess(QCommonResultBean<VerifyResult> response) {
                if (iPayExecResultListener != null) {
                    iPayExecResultListener.onSuccess(new IPayConsumeListener() {
                        @Override
                        public void onSuccess() {
                            paySuccess(response.getBody());
                        }

                        @Override
                        public void onFail() {
                            paySuccess(response.getBody());
                        }
                    });
                }else{
                    paySuccess(response.getBody());
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (iPayExecResultListener != null) iPayExecResultListener.onFail();
                payFail(code, msg);
            }
        });
    }

    private void paySuccess(VerifyResult result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                LogManager.getInstance().purchase(PayActivity.this, result.getCurrency(), result.getAmount(), result.getProduct_id());
                if (result.getSev() > 0) {
                    LogManager.getInstance().purchaseSev(PayActivity.this, result.getSev_currency(), result.getSev(), result.getProduct_id());
                }
                hideLoading();
                QSDK.getInstance().paymentSuccess();
                finish();
            }
        });
    }

    private void payFail(int code, String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                QSDK.getInstance().paymentFail(code, msg);
                finish();
            }
        });
    }

    private void payCancel() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                QSDK.getInstance().paymentCancel();
                finish();
            }
        });
    }

    public static void start(Activity activity, PayParams payParams) {
        PayActivity.mPayParams = payParams;
        Intent intent = new Intent(activity, PayActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        payCancel();
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        mPayParams = null;
        super.onDestroy();
    }
}
